﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Web.Feed
{
    public class FeedRepository<T> : IFeedRepository<T>
        where T : EventArgs
    {
        #region Constructor

        public FeedRepository(IFeedService<T> feedService)
        {
            _feedService = feedService;
        }
        
        #endregion

        #region Methods

        public Feed<T> GetRecentFeed()
        {
            if (_feedService.RecentFeedId != null)
                return Feed<T>.FromFeedId(_feedService.RecentFeedId, _feedService.Builder);
            else return Feed<T>.FromFileName(FeedDirectory.Current, _feedService.FileSystem.GetLatestFeed(), _feedService.Builder);
        }

        public Feed<T> GetFeed(FeedId feedId)
        {
            return Feed<T>.FromFeedId(feedId, _feedService.Builder);
        }

        public SyndicationItem GetEvent(Guid eventId)
        {
            if (!(_feedService.FileSystem.IsExists(FeedDirectory.Entry, eventId.ToString() + ".atom")))
                return null;

            using (var reader = _feedService.FileSystem.GetXmlReader(FeedDirectory.Entry, eventId.ToString() + ".atom"))
            {
                return SyndicationItem.Load(reader);
            }
        }
        
        #endregion

        #region Data

        IFeedService<T> _feedService = null;

        #endregion

        #region Public Properties
        
        #endregion



     
    }
}
